<?php
/*
 *   This file is part of verbum.
 *
 *   verbum is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   verbum is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with verbum.  If not, see <http://www.gnu.org/licenses/>.
 *   
 *   Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 * */
 
$cadena = dirname(__FILE__);
$fpos = strrpos($cadena, "/");
if ($fpos<=0) {
	$cadena = str_replace("\\","/",$cadena);
	$fpos = strrpos($cadena, "/");
}

define("AVOID_SESSION","true");
define("BASH_PATH",substr($cadena, 0, $fpos)."/");
define("LOG4PHP_CONFIGURATION",BASH_PATH."bash/logger.xml");	

require_once(BASH_PATH."includes/init.php");
//date_default_timezone_set("America/Argentina/Buenos_Aires"); scripts need UTC
$TRANSID = util::createTransactionId();
$_log =& LoggerManager::getLogger('bash');
//$blog =& LoggerManager::getLogger('bash');

function startlog($script) {
	global $loader;
	global $TRANSID;
	$sql = "INSERT INTO ".BASH_LOG." (transid,script,starttime) VALUES (".util::str($TRANSID).",".util::str($script).",".util::str(util::now()).")";
	return $loader->executeInsert($sql);
}

function endlog($id) {
	global $loader;
	$sql = "UPDATE ".BASH_LOG." set endtime = ".util::str(util::now())." WHERE id = ".util::str($id);
	$loader->execute($sql);
} 

function mtime() {
   $mtime = microtime();
   $mtime = explode(" ",$mtime);
   $mtime = $mtime[1] + $mtime[0];
   return $mtime; 	
}

function sec2hms ($sec, $padHours = false) 
  {

    // holds formatted string
    $hms = "";
    
    // there are 3600 seconds in an hour, so if we
    // divide total seconds by 3600 and throw away
    // the remainder, we've got the number of hours
    $hours = intval(intval($sec) / 3600); 

    // add to $hms, with a leading 0 if asked for
    $hms .= ($padHours) 
          ? str_pad($hours, 2, "0", STR_PAD_LEFT). ':'
          : $hours. ':';
     
    // dividing the total seconds by 60 will give us
    // the number of minutes, but we're interested in 
    // minutes past the hour: to get that, we need to 
    // divide by 60 again and keep the remainder
    $minutes = intval(($sec / 60) % 60); 

    // then add to $hms (with a leading 0 if needed)
    $hms .= str_pad($minutes, 2, "0", STR_PAD_LEFT). ':';

    // seconds are simple - just divide the total
    // seconds by 60 and keep the remainder
    $seconds = intval($sec % 60); 

    // add to $hms, again with a leading 0 if needed
    $hms .= str_pad($seconds, 2, "0", STR_PAD_LEFT);

    // done!
    return $hms;
    
}

?>
